package top.continew.minithreadpool.ui.commonUi;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;

public class TaskQueuePanel extends JPanel {
    private static final int TASK_WIDTH = 60;
    private static final int TASK_HEIGHT = 30;
    private static final int TASK_SPACING = 10;
    private static final int ANIMATION_SPEED = 5;

    private BlockingQueue<Runnable> queue;
    private List<TaskItem> taskItems = new ArrayList<>();
    private int maxVisibleTasks;

    public TaskQueuePanel(BlockingQueue<Runnable> queue) {
        this.queue = queue;
        setBackground(new Color(240, 240, 240));
        setBorder(BorderFactory.createTitledBorder("任务队列"));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 计算最大可见任务数
        maxVisibleTasks = getWidth() / (TASK_WIDTH + TASK_SPACING);

        // 绘制传送带背景
        drawConveyorBelt(g2d);

        // 绘制任务
        drawTasks(g2d);
    }

    private void drawConveyorBelt(Graphics2D g2d) {
        int beltHeight = TASK_HEIGHT + 20;
        int beltY = getHeight() / 2 - beltHeight / 2;

        // 绘制传送带主体
        g2d.setColor(new Color(200, 200, 200));
        g2d.fillRect(0, beltY, getWidth(), beltHeight);

        // 绘制传送带滚轮
        g2d.setColor(Color.DARK_GRAY);
        int wheelSize = 15;
        int wheelSpacing = 20;
        for (int i = 0; i < getWidth() / wheelSpacing; i++) {
            int wheelX = i * wheelSpacing + (int)(System.currentTimeMillis() / 50 % wheelSpacing);
            g2d.fillOval(wheelX, beltY - wheelSize/2, wheelSize, wheelSize);
            g2d.fillOval(wheelX, beltY + beltHeight - wheelSize/2, wheelSize, wheelSize);
        }

        // 绘制传送带边缘
        g2d.setColor(new Color(150, 150, 150));
        g2d.drawLine(0, beltY, getWidth(), beltY);
        g2d.drawLine(0, beltY + beltHeight, getWidth(), beltY + beltHeight);
    }

    private void drawTasks(Graphics2D g2d) {
        int startX = 10;
        int y = getHeight() / 2 - TASK_HEIGHT / 2;

        // 同步任务列表
        List<Runnable> currentTasks = new ArrayList<>(queue);
        if (currentTasks.size() > maxVisibleTasks) {
            currentTasks = currentTasks.subList(0, maxVisibleTasks);
        }

        // 更新任务项
        if (taskItems.size() != currentTasks.size()) {
            taskItems.clear();
            for (int i = 0; i < currentTasks.size(); i++) {
                taskItems.add(new TaskItem(i));
            }
        }

        // 绘制任务
        for (int i = 0; i < taskItems.size(); i++) {
            TaskItem taskItem = taskItems.get(i);
            int x = startX + i * (TASK_WIDTH + TASK_SPACING);

            // 更新位置（平滑移动）
            if (taskItem.targetX != x) {
                taskItem.targetX = x;
            }

            if (taskItem.currentX < taskItem.targetX) {
                taskItem.currentX = Math.min(taskItem.currentX + ANIMATION_SPEED, taskItem.targetX);
            } else if (taskItem.currentX > taskItem.targetX) {
                taskItem.currentX = Math.max(taskItem.currentX - ANIMATION_SPEED, taskItem.targetX);
            }

            // 绘制任务方块
            drawTask(g2d, (int) taskItem.currentX, y, i);
        }
    }

    private void drawTask(Graphics2D g2d, int x, int y, int taskId) {
        // 绘制任务方块
        RoundRectangle2D rect = new RoundRectangle2D.Double(x, y, TASK_WIDTH, TASK_HEIGHT, 10, 10);
        g2d.setColor(getTaskColor(taskId));
        g2d.fill(rect);

        // 绘制边框
        g2d.setColor(Color.BLACK);
        g2d.draw(rect);

        // 绘制任务ID
        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("Arial", Font.BOLD, 12));
        String text = "任务 " + taskId;
        FontMetrics fm = g2d.getFontMetrics();
        int textWidth = fm.stringWidth(text);
        int textX = x + (TASK_WIDTH - textWidth) / 2;
        int textY = y + (TASK_HEIGHT - fm.getHeight()) / 2 + fm.getAscent();
        g2d.drawString(text, textX, textY);
    }

    private Color getTaskColor(int taskId) {
        // 使用HSL颜色模型生成不同色调的颜色
        float hue = (taskId * 0.618f) % 1.0f; // 黄金分割分布
        return Color.getHSBColor(hue, 0.7f, 0.9f);
    }

    public void refresh() {
        repaint();
    }

    private static class TaskItem {
        double currentX;
        int targetX;

        TaskItem(int index) {
            currentX = index * (TASK_WIDTH + TASK_SPACING) + 10;
            targetX = (int) currentX;
        }
    }
}
